//
//  RealTimeView.m
//  mendianjieke
//
//  Created by 响当当 on 7/30/19.
//  Copyright © 2019 dongzheng. All rights reserved.
//

#import "RealTimeView.h"
#import "RealTimeCell.h"

@interface RealTimeView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, copy)NSArray *infoArr;
@end

@implementation RealTimeView

- (NSArray *)infoArr
{
    if (!_infoArr)
    {
        _infoArr = [[NSArray alloc]init];
    }
    return _infoArr;
}

- (UITableView *)tableView
{
    if (!_tableView)
    {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(10*kUIScaleBasePlus,0,SCREEN_WIDTH-20*kUIScaleBasePlus,SCREEN_HEIGHT-TABBAR_HEIGHT-NAVIBAR_HEIGHT) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = 0;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
        
        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
        
        // 设置文字
        [header setTitle:@"加载更多" forState:MJRefreshStateIdle];
        [header setTitle:@"下拉刷新" forState:MJRefreshStatePulling];
        [header setTitle:@"Loading ..." forState:MJRefreshStateRefreshing];
        
        // 设置字体
        header.stateLabel.font = [UIFont systemFontOfSize:17];
        header.lastUpdatedTimeLabel.font = [UIFont systemFontOfSize:14];
        
        // 设置颜色
        header.stateLabel.textColor = LCHexColor(0x1f1f1f);
        header.lastUpdatedTimeLabel.textColor = [UIColor clearColor];
        
        // 设置刷新控件
        self.tableView.mj_header = header;
        
        //上拉刷新
        MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadNewFootData)];
        //设置上拉刷新属性
        [footer setTitle:@"加载更多" forState:MJRefreshStateIdle];
        [footer setTitle:@"Loading ..." forState:MJRefreshStateRefreshing];
        
        // 设置字体
        footer.stateLabel.font = [UIFont systemFontOfSize:17];
        
        // 设置颜色
        footer.stateLabel.textColor = LCHexColor(0x1f1f1f);
        
//        _tableView.mj_footer = footer;
    }
    return _tableView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self addSubview:self.tableView];
    }
    
    return self;
}

#pragma mark - delegate & datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.infoArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 317*kUIScaleBasePlus;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 35*kUIScaleBasePlus;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-20*kUIScaleBasePlus,35*kUIScaleBasePlus)];
    headView.backgroundColor = [UIColor clearColor];
    UILabel *timeLabel = [Utilties creatLabelWithFrame:CGRectMake((SCREEN_WIDTH-20*kUIScaleBasePlus)/2-50*kUIScaleBasePlus,10*kUIScaleBasePlus,100*kUIScaleBasePlus,15*kUIScaleBasePlus)
                                          textAligment:NSTextAlignmentCenter
                                              textClor:LCHexColor(0xFFFFFF)
                                              textFont:10*kUIScaleBasePlus];
    timeLabel.backgroundColor = LCHexColor(0xC2C2C2);
    timeLabel.text = @"03-12 12:50";
    [headView addSubview:timeLabel];
    
    return headView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    RealTimeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RealTimeCell"];
    if (!cell)
    {
        cell = [[RealTimeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"RealTimeCell"];
        cell.selectionStyle = 0;
    }
    
    RealTimeModel *model = [[RealTimeModel alloc]initWithDic:self.infoArr[indexPath.section]];
    
    cell.model = model;
    
    return cell;
}

#pragma mark - Method
- (void)loadNewData
{
    WeakSelf(self)
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [weakSelf.tableView.mj_header endRefreshing];
    });
}

- (void)loadNewFootData
{
    WeakSelf(self)
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [weakSelf.tableView.mj_footer endRefreshing];
        
    });
    
}

- (void)getInfo:(NSArray *)info
{
    self.infoArr = info;
    [self.tableView reloadData];
}
@end
